<template>
	<template>
	  <button type="primary" @click="C">驾驶证识别</button>
	  <view class="result-item">
	  	<text>姓名：{{name || '未识别'}}</text>
	  </view>
	  <view class="result-item">
	  	<text>性别：{{sex || '未识别'}}</text>
	  </view>
	  <view class="result-item">
	  	<text>国籍：{{nationality || '未识别'}}</text>
	  </view>
	  <view class="result-item">
	  	<text>住址：{{address || '未识别'}}</text>
	  </view>
	  <view class="result-item">
	  	<text>证号：{{idNum || '未识别'}}</text>
	  </view>
	  <view class="result-item">
	  	<text>出生日期：{{birthDate || '未识别'}}</text>
	  </view>
	  <view class="result-item">
	  	<text>初次领证日期：{{issueDate || '未识别'}}</text>
	  </view>
	  <view class="result-item">
	  	<text user-select>准驾车型：{{carClass || '未识别'}}</text>
	  </view>
	  <view class="result-item">
	  	<text>有效期限起始日：{{validFrom || '未识别'}}</text>
	  </view> 
	  <view class="result-item">
	  	<text>有效期限终止日：{{validTo || '未识别'}}</text>
	  </view>
	  <view class="result-item">
	  	<text>印章文字：{{officialSeal || '未识别'}}</text>
	  </view>
	   
	</template>
</template>

<script>
export default {
	data() {
		return {
			name: "",
			sex: "",
			nationality: "",
			address: "",
			idNum: "",
			birthDate: "",
			issueDate: "",
			carClass: "",
			validFrom: "",
			validTo: "",
			officialSeal: "" 
		}
	},
	methods: {
		C() {
			this.photo("C")
		},
		photo(type) {
			let that = this
			wx.chooseMedia({
				count: 1,
				mediaType: ['image', 'video'],
				sourceType: ['album', 'camera'],
				success(res) {
					// tempFilePath可以作为img标签的src属性显示图片
					let imgUrl = res.tempFiles[0].tempFilePath;
					console.log("res.tempFiles[0].tempFilePath" + res.tempFiles[0].tempFilePath)
					console.log("res.tempFiles[0].size)" + res.tempFiles[0].size)
					that.uploadImg(type, imgUrl)
				}
			})
		},
		uploadImg(type, imgUrl) {
			let that = this
			wx.cloud.uploadFile({
				cloudPath: 'ocr/' + type + '.png',
				filePath: imgUrl, // 文件路径
				success: res => {
					console.log("上传成功", res.fileID)
					that.getImgUrl(type, res.fileID)
				},
				fail: err => {
					console.log("上传失败", err)
				}
			})
		},
		getImgUrl(type, imgUrl) {
			let that = this
			wx.cloud.getTempFileURL({
				fileList: [imgUrl],
				success: res => {
					let imgUrl = res.fileList[0].tempFileURL
					console.log("获取图片url成功:", imgUrl + "获取图片type成功:" + type)
					that.shibie(type, imgUrl)
				},
				fail: err => {
					console.log("获取图片url失败", err)
				}
			})
		},
		shibie(type, imgUrl) {
			wx.cloud.callFunction({
				name: "quickstartFunctions",
				data: {
					type: type,
					imgUrl: imgUrl
				},
				success: (res) => {
					this.name = res.result.name,
					this.sex= res.result.sex,
					this.nationality=res.result.nationality,
					this.address=res.result.address,
					this.idNum=res.result.idNum,
					this.birthDate=res.result.birthDate,
					this.issueDate=res.result.issueDate,
					this.carClass= res.result.carClass,
					this.validFrom = res.result.validFrom,
					this.validTo= res.result.validTo,
					this.validPeriod=res.result.validPeriod,
					this.officialSeal=res.result.officialSeal 
					console.log("识别成功", res)
				},
				fail(res) {
					console.log("识别失败", res)
				}
			})
		}
		
		
	}
	}
</script>

<style>
</style>